<?php
/**
 * 频道
 */
//require_once dirname(dirname(__DIR__))."/common.php";
require_once dirname(__DIR__)."/login_filter.php";
use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\model\HomePage;
//频道id
$pindao_id=$_REQUEST["pindao_id"];
if(empty($pindao_id)){
    Response::output(-1,"缺少参数");
}

//先从缓存里读取数据
if($_GET['debug']){echo "a:".microtime(true)."\n";}
$redis = SysResource::getRedis();
$redis_cache_key=REDIS_PREFIX."HOME_PINDAO_LIST_".$pindao_id;
$redis_cache_data=$redis->get($redis_cache_key);
if($redis_cache_data){
    //读取缓存里的数据
    $data=json_decode($redis_cache_data);
    Response::output(0,"查询成功1",$data,count($data));
}else {
    //查询数据库
    //分页
    $page = isset($_REQUEST["page"]) ? intval($_REQUEST["page"]) : 1;
    $page = $page > 0 ? $page : 1;
    $pageSize =10;

    $uuid=$Auth['uuid'];
    //实例化数据库
    $db = SysResource::getDB(false);
    $HomePage = new HomePage($db);
//先去查询有几个频道
    $res1=$HomePage->get_pindao_list($pindao_id);
    if(!empty($res1)){
        $res=[];
        //如果不是空的频道
        foreach($res1 as $k=>$v){
            $res2=$HomePage->get_pindao_material($v['typesub_id'],$page,$pageSize);
            $res=array_merge($res2['data'],$res);
        }
        if(!empty($res)){
            $zans=[];
            $zan_id=[];
            $guanzus=[];
            //数据库查询已点赞视频
            $da1=$HomePage->zan($uuid);
            foreach($da1 as $k=>$v){
                $zans[]=$v['video_uuid'];
                $zan_id[]=$v['id'];
            }
            //数据库查询已关注视频
            $da2=$HomePage->guanzu($uuid);
            foreach($da2 as $kk=>$vv){
                $guanzus[]=$vv['other_uuid'];
            }
            foreach($res as $kk=>&$vv){
                if(in_array($vv['video_uuid'],$zans) && in_array($vv['id'],$zan_id)){
                    $vv['is_zan']=2;
                }else{
                    $vv['is_zan']=1;
                }
                if(in_array($vv['uuid'],$guanzus)){
                    $vv['is_follow']=2;
                }else{
                    $vv['is_follow']=1;
                }
                if($vv['uuid']==$uuid){
                    $vv['is_user']=1;
                }else{
                    $vv['is_user']=0;
                }
                if(empty($vv['share_pic'])){
                    $vv['share_pic']="";
                }
                if(empty($vv['video_desc'])){
                    $vv['video_desc']="";
                }
                $vv['count_material_love_10']=round($vv['count_material_love']*1.7);
                $vv['count_material_share_10']=round($vv['count_material_share']*1.7);
            }
            $redis->setex($redis_cache_key,15,json_encode($res));
            Response::output(0,"查询成功2",$res,count($res));
        }else{
            Response::output(0,"查询成功3",[],0);
        }
    }else{
        Response::output(0,"查询成功4",[],0);
    }

}


